package kr.co.fashionmai.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import kr.co.fashionmai.service.FrontService;
import kr.co.fashionmai.util.PageUtil;
import kr.co.fashionmai.vo.OrderSheet;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class FrontController 
{
	private FrontService frontService;
	
	public void setFrontService(FrontService frontService)
	{
		this.frontService = frontService;
	}
	
	@RequestMapping(value="orderSheet.do")
	public ModelAndView orderSheet(OrderSheet orderSheet)
	{
		ModelAndView modelAndView = new ModelAndView("orderSheet");
		int totalCount = frontService.getTotalCount(orderSheet);
		
		int pageNo = orderSheet.getPageNo();
		if(pageNo==0) pageNo=1;
		
		PageUtil pageUtil = new PageUtil(pageNo, totalCount);
		modelAndView.addObject("pasing", pageUtil.getPageStr());
		
 		int queryPageNo = (pageNo-1)*20;
 		orderSheet.setPageNo(queryPageNo);
		
		List<OrderSheet> orderSheetList = frontService.getOrderSheetList(orderSheet);
		orderSheet.setPageNo(pageNo);
		modelAndView.addObject("orderSheetList", orderSheetList);
		return modelAndView;
	}
	
	@RequestMapping(value="orderSheetInsert.do")
	public String insert(OrderSheet orderSheet, @RequestParam(value="file", required=false) MultipartFile file) throws IllegalStateException, IOException
	{		
		if(!file.isEmpty())
		{
			String fileName = file.getOriginalFilename();
			int offset = fileName.lastIndexOf(".");
			String tempName = fileName.substring(offset);
//			String photo_uri = "C://java_soft/workspace/spring/FashionMai/WebContent/upload/"+orderSheet.getTitleNo()+tempName;
			String photo_uri = "/home/hosting_users/ii_fashionmai/www/upload/"+orderSheet.getTitleNo()+tempName;
			file.transferTo(new File(photo_uri));
			orderSheet.setPhoto_uri(orderSheet.getTitleNo()+tempName);
		}
		boolean bool = frontService.insert(orderSheet);
		if(!bool) return "redirect:orderSheet.do";
		return "redirect:orderSheet.do";
	}
	
	@RequestMapping(value="orderSheetUpdate.do") 
	public String update(OrderSheet orderSheet, @RequestParam(value="file", required=false) MultipartFile file, 
			@RequestParam(value="originTitleNo") String originTitleNo) throws IllegalStateException, IOException
	{
		if(!file.isEmpty())
		{
			String fileName = file.getOriginalFilename();
			int offset = fileName.lastIndexOf(".");
			String tempName = fileName.substring(offset);
//			String photo_uri = "C://java_soft/workspace/spring/FashionMai/WebContent/upload/"+orderSheet.getTitleNo()+tempName;
			String photo_uri = "/home/hosting_users/ii_fashionmai/www/upload/"+orderSheet.getTitleNo()+tempName;
			File remove_file = new File(photo_uri);
			if(remove_file.exists()) remove_file.delete();
			
			file.transferTo(new File(photo_uri));
			orderSheet.setPhoto_uri(orderSheet.getTitleNo()+tempName);
		}
		
		OrderSheet temp = frontService.select(orderSheet.getTitleNo());
		String photo = temp.getPhoto_uri();
		
		if(orderSheet.getPhoto_uri() == null || orderSheet.getPhoto_uri() == "")
		{
			orderSheet.setPhoto_uri(photo);
		}
		
		frontService.delete(originTitleNo);
		boolean bool = frontService.insert(orderSheet);
		if(!bool) return "redirect:orderSheet.do";
		return "redirect:orderSheet.do";
	}
	
	@RequestMapping(value="orderSheetDelete.do")
	public String delete(@RequestParam(value="titleNo") String titleNo)
	{
		OrderSheet orderSheet = frontService.select(titleNo);
		
		String photo_uri = "/home/hosting_users/ii_fashionmai/www/upload/"+orderSheet.getPhoto_uri();
//		String photo_uri = "C://java_soft/workspace/spring/FashionMai/WebContent/upload/"+orderSheet.getPhoto_uri();
		File file = new File(photo_uri);
		if(file.exists()) file.delete();
		
		frontService.delete(titleNo);
		return "redirect:orderSheet.do";
	}
	
	@RequestMapping(value="orderSheetUpdateSelect.do")
	public void updateSelect(@RequestParam(value="titleNo") String titleNo, HttpServletResponse response) throws IOException
	{
		response.setCharacterEncoding("UTF8");
		
		OrderSheet orderSheet = frontService.updateSelect(titleNo);
		JSONObject json = new JSONObject();
			json.put("titleNo", orderSheet.getTitleNo());
			json.put("customer", orderSheet.getCustomer());
			json.put("orderDate", orderSheet.getOrderDate());
			json.put("dueDate", orderSheet.getDueDate());
			json.put("won", orderSheet.getWon());
			json.put("yen", orderSheet.getYen());
			json.put("totalAmount", orderSheet.getTotalAmount());
			json.put("color", orderSheet.getColor());
			json.put("mix", orderSheet.getMix());
		
		PrintWriter writer = response.getWriter();
			writer.write(json.toString());
			writer.close();
	}
	
	@RequestMapping(value="orderSheetSelect.do")
	public void select(@RequestParam(value="titleNo") String titleNo, HttpServletResponse response) throws IOException
	{
		response.setCharacterEncoding("UTF8");
		OrderSheet orderSheet = frontService.select(titleNo);
		JSONObject json = new JSONObject();
			json.put("photo_uri",orderSheet.getPhoto_uri());
			json.put("color",orderSheet.getColor());
			json.put("mix",orderSheet.getMix());
			 
		PrintWriter writer = response.getWriter();
			writer.write(json.toString());
			writer.close();
	}
	
	@RequestMapping(value="orderSheetTitleCheck.do")
	public void titleCheck(@RequestParam(value="titleNo") String titleNo, HttpServletResponse response) throws IOException
	{
		String result = frontService.titleCheck(titleNo);
		PrintWriter writer = response.getWriter();
		
		JSONObject json = new JSONObject();
		
		if(result == null) json.put("result","1");
		else json.put("result","2");
		
		writer.write(json.toString());
		writer.close();
	}
}








